package com.qianzhou.eduWeb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qianzhou.dto.CourseCommentDTO;
import com.qianzhou.eduService.service.CourseCommentService;
import com.qianzhou.vo.CourseCommentVO;
import com.qianzhou.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: qian.zhou
 * @Description: 课程评论接口
 * @Date Created in 2021-08-30-22:00
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("course")
@Api(tags = {"课程评论接口"})
public class CourseCommentController {
    @Reference
    private CourseCommentService courseCommentService;

    @ApiOperation("添加课程评论")
    @RequestMapping(value = "comment/saveCourseComment", method = {RequestMethod.POST})
    public ResponseVO<Object> saveCourseComment(Integer courseId, Integer userid, String username, String comment) throws UnsupportedEncodingException {
        username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
        comment = new String(comment.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println("昵称：" + username);
        CourseCommentDTO courseComment = new CourseCommentDTO();
        courseComment.setCourseId(courseId); // 课程编号
        courseComment.setSectionId(0); // 章节编号 （预留字段，为项目的2.0版本保留）
        courseComment.setLessonId(0);// 小节编号（预留字段，为项目的2.0版本保留）
        courseComment.setUserId(userid); // 用户编号
        courseComment.setUserName(username); // 用户昵称
        courseComment.setParentId(0); //没有父id（预留字段，为项目的2.0版本保留）
        courseComment.setComment(comment);// 留言内容
        courseComment.setType(0); // 0用户留言（预留字段，为项目的2.0版本保留）
        courseComment.setLastOperator(userid); //最后操作的用户编号
        Integer i = courseCommentService.saveCourseComment(courseComment);
        if (i == null) {
            return new ResponseVO<>(500, "保存评论失败", null);
        }
        return new ResponseVO<>(2000, "保存评论成功", null);
    }

    @ApiOperation("获取课程评论")
    @RequestMapping(value = "comment/getCourseCommentList/{courseId}/{pageIndex}/{pageSize}", method = {RequestMethod.GET})
    public ResponseVO<Object> getCommentsByCourseId(@PathVariable("courseId") Integer courseId, @PathVariable("pageIndex") Integer pageIndex, @PathVariable("pageSize") Integer pageSize) {
        int offset = (pageIndex - 1) * pageSize;
        List<CourseCommentVO> courseCommentVOS = courseCommentService.getCommentsByCourseId(courseId, offset, pageSize);
        System.out.println("获取第" + courseId + "门课程的留言：共计" + courseCommentVOS.size() + "条");
        return new ResponseVO<>(2000, "响应成功", courseCommentVOS);
    }

    @ApiOperation("点赞")
    @RequestMapping(value = "comment/saveFavorite/{commentId}/{userid}", method = {RequestMethod.POST})
    public ResponseVO<Object> saveFavorite(@PathVariable("commentId") Integer commentId, @PathVariable("userid") Integer userid) {
        Integer integer = courseCommentService.saveFavorite(commentId, userid);
        if (integer == null) {
            return new ResponseVO<>(500, "点赞失败", null);
        }
        return new ResponseVO<>(2000, "点赞成功", null);
    }

    @ApiOperation("取消点赞")
    @RequestMapping(value = "comment/cancelFavorite/{commentId}/{userid}", method = {RequestMethod.POST})
    public ResponseVO<Object> cancelFavorite(@PathVariable("commentId") Integer commentId, @PathVariable("userid") Integer userid) {
        Integer integer = courseCommentService.cancelFavorite(commentId, userid);
        if (integer == null) {
            return new ResponseVO<>(500, "取消失败", null);
        }
        return new ResponseVO<>(2000, "取消成功", null);
    }
}
